package com.ygqh.baby.controller.admin;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ygqh.baby.ao.ContractStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.ao.SettlementMode;
import com.ygqh.baby.ao.SettlementStatus;
import com.ygqh.baby.po.AdminSettlement;
import com.ygqh.baby.service.AdminContractService;
import com.ygqh.baby.service.AdminSettlementService;
import com.ygqh.baby.utils.JsonUtils;


@Controller
@RequestMapping("/admin/settlement")
public class SettlementController  extends BaseController<Object, java.lang.Long>{
	
    @Autowired
	private AdminSettlementService adminSettlementService;	
    @Autowired
   	private AdminContractService adminContractService;	
   	
	@RequestMapping("searchSettlement")
	@ResponseBody
	public Map<String,Object> searchSettlement(String q,QueryInfo queryInfo,String supplierName,String contractCode,Date startDate,Date endDate,SettlementStatus status) {	
		
		ResultSet<AdminSettlement> resultSet=adminSettlementService.search(queryInfo,q,null,supplierName,contractCode,startDate,endDate,status);
		
		return resultSet.toDatatables(null);
	}
	@RequestMapping("searchSettlementList")
	@ResponseBody
	public List<AdminSettlement> searchSettlementList(String q,QueryInfo queryInfo) {	
		
		return adminSettlementService.find();
	}
	
	@RequestMapping("saveSettlement")
	@ResponseBody
	public Message saveSettlement(String settlement) {
		AdminSettlement adminSettlement=JsonUtils.toObject(settlement, AdminSettlement.class);		
		if(!adminSettlement.getSettlementMode().equals(SettlementMode.SecondMoney.name()))
				adminSettlement.setPurchaseCode(null);
		if(adminSettlement.getId()!=null){
			adminSettlement.setUpdateBy(getSession().getTrueName());
			adminSettlement.setUpdateTime(new Date());
		}else{
			adminSettlement.setCreateBy(getSession().getTrueName());
			adminSettlement.setCreateTime(new Date());
			adminSettlement.setSettlementStatus(SettlementStatus.NoSettlement);
			
		}		
		
	   return adminSettlementService.saveOrUpdate(adminSettlement)==1?SUCCESS_MESSAGE:ERROR_MESSAGE;
	
	}
	
	@RequestMapping("getSettlementById")
	@ResponseBody
	public AdminSettlement getSettlementById(Long id) {
		return adminSettlementService.findSettlementById(id);
	
	}
	@RequestMapping("searchContractSettlement")
	@ResponseBody
	public Map<String,Object> searchContractSettlement(String q,QueryInfo queryInfo,String supplierName,String contractCode,ContractStatus contractStatus) {	
		
		ResultSet<Map<String,Object>> resultSet=adminContractService.searchContractSettlement(queryInfo, contractCode, supplierName, contractStatus);
		
		return resultSet.toDatatables(null);
	}
}
